package cate.game.role.farm.cross_boss;

import cate.common.table.cross_boss.base.CrossBossFarmBaseRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.farm.local.cross_boss.data.CrossBossDetailVO;
import cate.game.farm.local.cross_boss.data.CrossBossOverallVO;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarm;
import cate.game.role.farm.PlayTimes;
import cate.game.role.farm.cross_boss.msg.CrossBossOverallInfoResp;
import cate.game.role.farm.cross_boss.msg.CrossBossViewDetailResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@NoteClass("玩家跨服BOSS数据")
public class MyCrossBossPO extends AbstractFarm {
    @NoteField("挑战次数")
    public PlayTimes playTimes;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (playTimes == null) {
            playTimes = new PlayTimes();
        }
        playTimes.initialize();
    }

    @Override
    public int getFuncId() {
        return GDFunc.CROSS_BOSS_FARM;
    }

    @Override
    public void onAfterOpen() {
        initSend();
    }

    @Override
    public int getFightFuncId() {
        return GDFunc.CROSS_BOSS_FARM;
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        if (playTimes != null) {
            playTimes.reset();
            if (!silence) {
                initSend();
            }
        }
    }

    /**
     * 初始化发送
     */
    public void initSend() {
        if (!isOpen()) {
            return;
        }
        sendOverallInfo(role.getGame().farm.crossBoss.getCrossBossOverall());
    }

    /**
     * 发送汇总简要信息
     *
     * @param vo 信息
     */
    public void sendOverallInfo(CrossBossOverallVO vo) {
        if (vo == null) {
            return;
        }
        role.sendNow(new CrossBossOverallInfoResp(playTimes, vo));
    }

    /**
     * 查看层具体信息
     *
     * @param id BOSS配置ID
     * @return 查看结果
     */
    public EcResult<Void> viewBossDetail(int id) {
        EcResult<Void> r = new EcResult<>();
        CrossBossDetailVO vo = role.getGame().farm.crossBoss.getCrossBossDetail(id, role);
        if (vo == null) {
            return r.fail("查看失败");
        }
        role.sendNow(new CrossBossViewDetailResp(vo));
        return r.success();
    }

    /**
     * 尝试战斗
     *
     * @param id
     * @return
     */
    public EcResult<Void> tryFightBoss(int id) {
        EcResult<Void> r = new EcResult<>();
        if (isFighting()) {
            return r.fail("战斗进行中");
        }
        CrossBossFarmBaseRow baseRow = role.getGame().table.crossBossFarm.base.get(id);
        if (baseRow == null) {
            return r.fail("找不到该BOSS配置");
        }
        r = playTimes.available(role, getFuncId());
        if (!r.ok()) {
            return r;
        }
        if (baseRow.conditionStar > 0 && baseRow.conditionNum > 0) {
            long count = role.getBag().hero.getList().stream()
                    .filter(hero -> hero.getTpl(role.getGame()).star >= baseRow.conditionStar)
                    .count();
            if (count < baseRow.conditionNum) {
                return r.fail("不满足挑战条件");
            }
        }
        EcResult<FightSide> fsr = role.getGame().fight.side.createFightSideByCode(GDFight.Team.B, baseRow.insCode);
        if (!fsr.ok()) {
            return r.fail(fsr.message);
        }
        long curHp = role.getGame().farm.crossBoss.tryFightCrossBoss(id);
        if (curHp < 0) {
            return r.fail("BOSS锁定中,请稍后尝试!");
        } else if (curHp == 0) {
            return r.fail("BOSS已被击杀!");
        }
        CrossBossFarmFTC context = new CrossBossFarmFTC(role, id);
        context.funcId = GDFunc.CROSS_BOSS_FARM;
        context.setPositionApply(GDFunc.ADVENTURE);
        context.param.sideB.hpExtend.recordByHeroTid(fsr.data.ps.hs.get(0).hero.tid, curHp / (double) baseRow.maxHp);
        if (curHp <= baseRow.crazyHp && baseRow.crazyBuff > 0) {
            context.param.sideB.skillAppend.addTeamSkill(baseRow.crazyBuff);
        }
        context.playStory.level = id;
        EcResult<PlayPO> fightRes = role.getGame().play.startByRole(role, context, fsr.data);
        if (!fightRes.ok()) {
            role.getGame().farm.crossBoss.reportFightResult(id, role, -1);
            return r.fail(fightRes.message);
        }
        long damage = fightRes.data.teamEnd.b.getAllHero().get(0).performance.hurt;
        role.getGame().farm.crossBoss.reportFightResult(id, role, damage);
        context.reward = new MixRes(baseRow.challengeRewardStr);
        playTimes.added();
        return r.success();
    }
}
